﻿using System;
using System.Net;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Ayatta.Contract.Domain;
using Web.Manage.Controllers;
using Ayatta.Contract.Component;

namespace Web.Manage.Areas.Game.Controllers
{
    public class ServerController : AdminBaseController
    {

        public ServerController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Detail(int? param, int gameId)
        {
            var model = new GameServer { GameId = gameId, Status = true, OpendOn = DateTime.Now.AddDays(2).Date };
            if (param.HasValue && param.Value > 0)
            {
                model = GameService.GetGameServer(param.Value);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Detail(int? param, int gameId, GameServer model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;
            if (model.JoinTrial)
            {
                if (!model.StartTime.HasValue || !model.EndTime.HasValue)
                {
                    return Json(HttpStatusCode.BadRequest, "请输入试玩开始与结束时间");
                }
                if (model.EndTime.Value <= model.StartTime.Value)
                {
                    return Json(HttpStatusCode.BadRequest, "试玩结束时间不能小于开始时间");
                }
            }
            if (param.HasValue && param.Value > 0)
            {
                var old = GameService.GetGameServer(param.Value);
                if (TryUpdateModel(old))
                {
                    if (!old.JoinTrial)
                    {
                        model.StartTime = null;
                        model.EndTime = null;
                    }
                    var result = GameService.UpdateGameServer(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", "/game/home/servers/", "/game/home/servers/" + gameId) : Json(HttpStatusCode.BadRequest, "更新失败", "/game/home/servers/", "/game/home/servers/" + gameId);
                }
            }
            else
            {
                model.GameId = gameId;
                model.ModifiedOn = dateTime;
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = GameService.CreateGameServer(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", "/game/home/servers/", "/game/home/servers/" + gameId);
                }
            }
            RemoveCache(CacheKey.Game.Games);
            return Json(HttpStatusCode.BadRequest, "失败");
        }
       
    }
}
